package com.simon.hse.activity.risk;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.simon.hse.MyApplication;
import com.simon.hse.R;
import com.simon.hse.activity.BaseActivity;
import com.simon.hse.adapter.CommonEmptyAdapter;
import com.simon.hse.adapter.CommonViewHolder;
import com.simon.hse.http.CommonCallBack;
import com.simon.hse.http.HttpManager;
import com.simon.hse.http.entity.CommonInput;
import com.simon.hse.http.entity.DangerEliminateOutput;
import com.simon.hse.http.entity.DropDownOutput;
import com.simon.hse.http.entity.OperationAndWorkListOutput;
import com.simon.hse.http.entity.RiskActivityInput;
import com.simon.hse.http.entity.TrainPersonOutput;
import com.simon.hse.interfaces.PopListener;
import com.simon.hse.utils.PopupWindowUtils;
import com.simon.hse.utils.ToastUtils;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;

/**
 * 操作及相关作业活动
 */
@SuppressWarnings("all")
@ContentView(R.layout.activity_risk_function1)
public class RiskActivityActivity extends BaseActivity implements PopListener {
    @ViewInject(R.id.tv_common_title)
    TextView mTv_common_title;

    @ViewInject(R.id.ll_more)
    LinearLayout mLl_more;

    @ViewInject(R.id.ll_function_1)
    LinearLayout ll_function_1;

    @ViewInject(R.id.tv_function_1)
    TextView tv_function_1;

    @ViewInject(R.id.ll_function_2)
    LinearLayout ll_function_2;

    @ViewInject(R.id.tv_function_2)
    TextView tv_function_2;

    @ViewInject(R.id.ll_function_3)
    LinearLayout ll_function_3;

    @ViewInject(R.id.tv_function_3)
    TextView tv_function_3;

    @ViewInject(R.id.ll_function_1_more)
    LinearLayout ll_function_1_more;

    @ViewInject(R.id.tv_function_1_more)
    TextView tv_function_1_more;

    @ViewInject(R.id.et_function_2_more)
    EditText et_function_2_more;

    @ViewInject(R.id.et_function_3_more)
    EditText et_function_3_more;

    @ViewInject(R.id.smartrefresh)
    SmartRefreshLayout mSmartRefresh;

    @ViewInject(R.id.recyclerview)
    RecyclerView mRecyclerView;

    private int currentPage = 1, totalPage = 1;
    private CommonEmptyAdapter mCommonEmptyAdapter;
    private List<OperationAndWorkListOutput.DataBean.PageDataBean> mDataBeanList = new ArrayList<>();// 操作列表
    private String riskId;// 风险类别id
    private String levelId;// 风险级别id

    private boolean isShowMore = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mTv_common_title.setText("操作及相关作业活动");

        initRecyclerView();
        initData();
    }

    @Event(value = {R.id.iv_common_left, R.id.ll_eliminate_more, R.id.ll_function_1, R.id.ll_function_2, R.id.ll_function_3})
    private void clickButton(View view) {
        switch (view.getId()) {
            case R.id.iv_common_left:
                finish();
                break;
            case R.id.ll_eliminate_more:
                if (isShowMore) {
                    mLl_more.setVisibility(View.GONE);
                } else {
                    mLl_more.setVisibility(View.VISIBLE);
                }
                isShowMore = !isShowMore;
                break;
            case R.id.ll_function_1:
//                initCommonPop(mLl_danger_eliminate_company, mTv_danger_eliminate_company, mDangerDepartList);
                break;
            case R.id.ll_function_2:
//                initPersonPop();
                break;
            case R.id.ll_function_3:
//                initPersonPop();
                break;
        }
    }

    @Event(value = {R.id.ll_danger_eliminate_more_rate, R.id.ll_danger_eliminate_more_area, R.id.ll_danger_eliminate_more_station, R.id.ll_danger_eliminate_more_standard
            , R.id.tv_more_confirm, R.id.tv_more_reset})
    private void more(View view) {
        switch (view.getId()) {
//            case R.id.ll_danger_eliminate_more_rate:
//                initCommonPop(mLl_danger_eliminate_more_rate, mTv_danger_eliminate_more_rate, mDangerRatesList);
//                break;
//            case R.id.ll_danger_eliminate_more_area:
//                initCommonPop(mLl_danger_eliminate_more_area, mTv_danger_eliminate_more_area, mDangerAreasList);
//                break;
//            case R.id.ll_danger_eliminate_more_station:
//                initCommonPop(mLl_danger_eliminate_more_station, mTv_danger_eliminate_more_station, mDangerStationsList);
//                break;
//            case R.id.ll_danger_eliminate_more_standard:
//                initCommonPop(mLl_danger_eliminate_more_standard, mTv_danger_eliminate_more_standard, mDangerStandardsList);
//                break;
            case R.id.tv_more_confirm:
                mLl_more.setVisibility(View.GONE);
                isShowMore = !isShowMore;
                break;
            case R.id.tv_more_reset:
                tv_function_1_more.setText("");
                tv_function_1_more.setHint("请选择");
                et_function_2_more.setText("");
                et_function_2_more.setHint("请输入风险");
                et_function_3_more.setText("");
                et_function_3_more.setHint("请输入危害因素");
                break;
        }
    }

    private void initRecyclerView() {
        mSmartRefresh.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                if (currentPage * 10 < totalPage) {
                    currentPage++;
                    initData();
                } else {
                    mSmartRefresh.finishLoadMoreWithNoMoreData();
                }
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                currentPage = 1;
                mDataBeanList.clear();
                initData();
            }
        });

        LinearLayoutManager llm = new LinearLayoutManager(RiskActivityActivity.this);
        mRecyclerView.setLayoutManager(llm);
        // 如果Item够简单，高度是确定的，打开FixSize将提高性能
        mRecyclerView.setHasFixedSize(true);
        // 设置Item默认动画，加也行，不加也行
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        mRecyclerView.setNestedScrollingEnabled(false);

        mCommonEmptyAdapter = new CommonEmptyAdapter(RiskActivityActivity.this, R.layout.layout_item_rist_function1, mDataBeanList);
        mCommonEmptyAdapter.setItemDataListener(new CommonEmptyAdapter.ItemDataListener<OperationAndWorkListOutput.DataBean.PageDataBean>() {
            @Override
            public void setItemData(CommonViewHolder holder, OperationAndWorkListOutput.DataBean.PageDataBean dataBean) {
                TextView tv_risk_title = holder.getView(R.id.tv_risk_title);
                TextView tv_risk_level = holder.getView(R.id.tv_risk_level);
                TextView tv_risk_task = holder.getView(R.id.tv_risk_task);

                tv_risk_title.setText(dataBean.getJobSteps());
                tv_risk_task.setText(dataBean.getJobTask().toString());
                String riskLevelText = dataBean.getRiskLevelText().toString();
                tv_risk_level.setText(riskLevelText);
                switch (riskLevelText) {
                    case "二级":
                        tv_risk_level.setBackgroundResource(R.drawable.rect_yellow_corner_5);
                        break;
                    case "三级":
                        tv_risk_level.setBackgroundResource(R.drawable.rect_red_corner_5);
                        break;
                }
            }
        });

        mCommonEmptyAdapter.setOnItemEmptyClickListener(new CommonEmptyAdapter.OnItemEmptyClickListener() {
            @Override
            public void setOnEmptyClickListener() {
                initData();
            }

            @Override
            public void setOnItemClickListener(View view, int position) {
                if (!isShowMore) {
                    Intent intent = new Intent(RiskActivityActivity.this, RiskActivityDetailActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putString("ID", mDataBeanList.get(position).getId());
                    intent.putExtras(bundle);
                    startActivity(intent);
                }
            }

            @Override
            public void setOnItemLongClickListener(View view, int position) {

            }
        });

        mRecyclerView.setAdapter(mCommonEmptyAdapter);
    }

    /**
     * 操作及相关作业活动-列表
     */
    private void initData() {
        RiskActivityInput riskActivityInput = new RiskActivityInput();
        RiskActivityInput.ModelBean modelBean = new RiskActivityInput.ModelBean();
        if (!TextUtils.isEmpty(riskId)) {
            modelBean.setFxTypeID(riskId);
        }
        if (!TextUtils.isEmpty(levelId)) {
            modelBean.setRiskLevel(levelId);
        }
//        String step = mEt_job_step.getText().toString();
//        if (!TextUtils.isEmpty(step)){
//            modelBean.setJobSteps(step);
//        }
//        String task = mEt_job_task.getText().toString();
//        if (!TextUtils.isEmpty(task)){
//            modelBean.setJobTask(task);
//        }

        riskActivityInput.setModel(modelBean);
        riskActivityInput.setLimit(10);
        riskActivityInput.setOffset(10 * (currentPage - 1));

        Call<String> call = HttpManager.getInstance().requestService().OPERATION_AND_WORK_LIST(HttpManager.getInstance().requestBody(riskActivityInput));
        HttpManager.getInstance().requestCallBack(call, new CommonCallBack<String>() {
            @Override
            public void requestSuccess(String result) {
                OperationAndWorkListOutput operationAndWorkListOutput = new Gson().fromJson(result, OperationAndWorkListOutput.class);
                if (operationAndWorkListOutput.getResult().equals("Success")) {
                    totalPage = operationAndWorkListOutput.getData().getRowCount();
                    List<OperationAndWorkListOutput.DataBean.PageDataBean> pageDataBeanList = operationAndWorkListOutput.getData().getPageData();
                    mDataBeanList.addAll(pageDataBeanList);
                    mCommonEmptyAdapter.notifyDataSetChanged();

                    mSmartRefresh.finishRefresh();
                    mSmartRefresh.finishLoadMore();
                } else {
                    ToastUtils.getInstance().showShortToastBottom(operationAndWorkListOutput.getMessage().toString());
                }
            }

            @Override
            public void requestError(String errorMsg) {
                ToastUtils.getInstance().showShortToastBottom(errorMsg);
            }
        });
    }

    @Override
    public void upData() {
        currentPage = 1;
        mDataBeanList.clear();
        initData();
    }
}
